package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.voicechat.firstmet.App;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.Params;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.text.ParseException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: MemberCenterActivity
 * @Description: 会员中心页面
 * @Author: ljp
 * @CreateDate: 2018/9/28 15:58
 * @Version: 1.0
 */
public class MemberCenterActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.rela_1)
    RelativeLayout rela1;
    @BindView(R.id.image_icon)
    CircleImageView imageIcon;
    @BindView(R.id.tv_nik_name)
    TextView tvNikName;
    @BindView(R.id.linear_member)
    LinearLayout linearMember;
    @BindView(R.id.tv_vip_state)
    TextView tvVipState;
    @BindView(R.id.tv_open_vip)
    TextView tvOpenVip;
    @BindView(R.id.linear_not_vip_rights)
    LinearLayout linearNotVipRights;
    @BindView(R.id.switch_cloaking)
    ToggleButton switchCloaking;
    @BindView(R.id.switch_browse)
    ToggleButton switchBrowse;
    @BindView(R.id.switch_cancel)
    ToggleButton switchCancel;
    @BindView(R.id.linear_vip_rights)
    LinearLayout linearVipRights;
    @BindView(R.id.tv_buy)
    TextView tvBuy;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String token;
    private String id;
    private String deviceInfo;
    private String TAG = "MemberCenterActivity";

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_member_center;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        id = mySeleInfo.getId();
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        initView();
    }
    /**
    * @method
    * @description 数据实例化
    * @date: 2018/10/31 19:30
    * @author: ljp
    * @return
    */
    private void initView() {
        initHttp();
       // setImage(mySeleInfo.getAvatarUrl(),imageIcon);
        GlideLoadUtils.getInstance().glideLoad(App.getContext(), OssConfig.OSS_HTTP
                + mySeleInfo.getAvatarUrl(), imageIcon);
        //判断我是否是会员
        if(mySeleInfo.getvType().equals("0")){//0不是会员
            linearNotVipRights.setVisibility(View.VISIBLE);
            linearVipRights.setVisibility(View.GONE);
            tvOpenVip.setText("立即开通");
        }else {
            linearNotVipRights.setVisibility(View.GONE);
            linearVipRights.setVisibility(View.VISIBLE);
            tvOpenVip.setText("立即续费");
        }
        tvNikName.setText(mySeleInfo.getNickName());
        //Log.i(TAG, "initView: ----"+mySeleInfo.toString());
        if(mySeleInfo.getRemind() == 0){
            switchCancel.setChecked(true);
        }else {
            switchCancel.setChecked(false);
        }
        if(mySeleInfo.getVagrancy() == 0){
            switchBrowse.setChecked(true);
        }else {
            switchBrowse.setChecked(false);
        }
        //取消关注提现
        switchCancel.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setHttpCancel();
            }
        });
        //无痕浏览
        switchBrowse.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setHttpBrowse();
            }
        });
    }
    //无痕浏览
    private void setHttpBrowse() {
        Params params = new Params();
        params.put("vagrancy",switchBrowse.isChecked()?"0":"1");
        params.put("userId", mySeleInfo.getId());
        params.put("mobile", SPUtils.getEditoString(SPUtils.SP_PHONE));//手机号码
        params.put("token", token);//token
        params.put("equipmentId",PhoneUtils.getDeviceInfo(context));//设备号
        MeInfoService.setVipRight(params).subscribe(result -> {
            //Log.i(TAG, "setHttpOnLineState: ----"+mySelfInfoBean.getCode());
            Log.i(TAG, "setHttpBrowse: ----"+result.toString());
            if (result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
                this.mySeleInfo.setVagrancy(switchBrowse.isChecked()?0:1);
                SPUtils.setEditorKeyValue(SPUtils.SP_MYSELFINFO, ParcelableUtils.marshall(mySeleInfo));
            }else if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(result.getMsg());
            }
        },throwable -> {
            toast_MakeText(getResources().getString(R.string.net_error));
        });

    }
    //取消关注提醒
    private void setHttpCancel() {
        Params params = new Params();
        params.put("remind",switchCancel.isChecked()?"0":"1");
        params.put("userId", mySeleInfo.getId());
        params.put("mobile", SPUtils.getEditoString(SPUtils.SP_PHONE));//手机号码
        params.put("token", token);//token
        params.put("equipmentId",PhoneUtils.getDeviceInfo(context));//设备号
        MeInfoService.setVipRight(params).subscribe(result -> {
            Log.i(TAG, "setHttpCancel: ----"+result.toString());
            if (result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
                this.mySeleInfo.setRemind(switchCancel.isChecked()?0:1);
                SPUtils.setEditorKeyValue(SPUtils.SP_MYSELFINFO, ParcelableUtils.marshall(mySeleInfo));
            }else if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(result.getMsg());
            }
        },throwable -> {
            toast_MakeText(getResources().getString(R.string.net_error));
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        initHttp();
    }

    /**
    * @method
    * @description 请求会员相关信息
    * @date: 2018/10/31 19:34
    * @author: ljp
    * @return
    */
    private void initHttp() {
        MeInfoService.queryVipData(token,deviceInfo,id).subscribe(mySelfInfoBean -> {
            if(mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
               Log.i(TAG, "initHttp: -----"+mySelfInfoBean.getObject().toString());
                initVipData(mySelfInfoBean.getObject());
            }else if(mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(mySelfInfoBean.getMsg());
            }
        },throwable -> {
            Log.i(TAG, "initHttp: --------"+throwable.toString());
        });
    }

    private void initVipData(MySelfInfoBean.ObjectBean object) {
        if(!mySeleInfo.getvType().equals(object.getvType())){
            mySeleInfo.setvType(object.getvType());
        }
        //判断我是否是会员
        if(object.getvType().equals("0")){//0不是会员
            linearNotVipRights.setVisibility(View.VISIBLE);
            linearVipRights.setVisibility(View.GONE);
            tvOpenVip.setText("立即开通");
        }else {
            linearNotVipRights.setVisibility(View.GONE);
            linearVipRights.setVisibility(View.VISIBLE);
            tvOpenVip.setText("立即续费");
        }
        if(!object.getvType().equals("0")) {
            try {
                String format = Utils.dateToString(Utils.stringToDate(object.getEndDate(), "yyyy-MM-dd HH:mm:ss"), "yyyy年MM月dd日");
                tvVipState.setText(format + "到期");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (object.getVagrancy() == 0) {  // 无痕浏览 0开启 1关闭
                switchBrowse.setChecked(true);
            } else {
                switchBrowse.setChecked(false);
            }
            if (object.getRemind() == 0) { // 取消关注提醒 0开启 1关闭
                switchCancel.setChecked(true);
            } else {
                switchCancel.setChecked(false);
            }
        }
    }


    @OnClick({R.id.linear_back, R.id.tv_open_vip})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv_open_vip:
                Intent intent = new Intent(activity, BuyVIPActivity.class);
                intent.putExtra("data",tvVipState.getText().toString().trim());
                startActivity(intent);
                finish();
                break;
        }
    }
    @OnClick(R.id.tv_buy)
    public void onClick() {
        startActivity(new Intent(activity,BuyVipRecordActivity.class));
    }
}
